package com.shun.common.utils;

import org.springframework.core.env.Environment;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * 环境变量服务工具类
 *
 * @author clever
 * @since 2021-09-26
 */
public class ShunEnvUtil {

    //API文档的连接地址
    private static final String DOCKET_URL = "https://www.baidu.com";
    //API文档的作者
    private static final String DOCKET_AUTHOR = "clever";
    //API文档的EMAIL
    private static final String DOCKET_EMAIL = "clever@163.com";

    /**
     * 获取服务名称
     *
     * @param env
     * @return
     */
    public static String getApplicationName(Environment env) {
        return env.getProperty("spring.application.name");
    }

    /**
     * 获取服务端口
     *
     * @param env
     * @return
     */
    public static String getServerPort(Environment env) {
        return env.getProperty("server.port");
    }

    /**
     * api文档创建bean
     *
     * @return
     */
    public static Docket createDocket(String title, String desc, String url, String author, String email, String basePackage,boolean flag) {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title(title)
                        .description(desc)
                        .termsOfServiceUrl(url)
                        .contact(new Contact(author, url, email))
                        .version("1.0")
                        .build())
                .groupName("1.0版本")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build();
        docket.enable(flag);
        return docket;
    }

    /**
     * api文档创建bean【简化版】
     * @return
     */
    public static Docket createDocket(String title, String desc, String basePackage,boolean flag) {
        return createDocket(title, desc, DOCKET_URL, DOCKET_AUTHOR, DOCKET_EMAIL, basePackage,flag);
    }
}
